package sanshui.system.trade.util;

import org.apache.logging.log4j.util.Strings;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

public class CommonTimeUtil {

    private static DateTimeFormatter yyyyMMdd_HHmmss = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
    private static DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");

    /**
     * yyyyMMdd HH:mm:ss
     **/
    public static String getNowTime() {
        LocalDateTime now = LocalDateTime.now();
        return now.format(yyyyMMdd_HHmmss);
    }

    /**
     * yyyyMMdd HH:mm:ss
     **/
    public static String getNowTime16() {
        LocalDateTime now = LocalDateTime.now();
        return now.format(yyyyMMddHHmmss);
    }

    /**
     * yyyyMMdd HH:mm:ss
     **/
    public static String getNowTime16(LocalDateTime now) {
        if(null == now){
            return null;
        }
        return now.format(yyyyMMddHHmmss);
    }

    /**
     * yyyyMMdd HH:mm:ss
     **/
    public static String getNowTime8() {
        LocalDateTime now = LocalDateTime.now();
        return now.format(yyyyMMdd);
    }

    /**
     * yyyyMMdd HH:mm:ss
     **/
    public static String getNowTime8(LocalDateTime now) {
        if(null == now){
            return null;
        }
        return now.format(yyyyMMdd);
    }

    public static String getNowTime8_CH(LocalDateTime now) {
        if(null == now){
            return null;
        }
        String format = now.format(yyyyMMdd);
        return format.substring(0, 4) + "年" + format.substring(4, 6) + "月" + format.substring(6, 8) + "日";
    }

    public static String getNowTimestamp() {
        Date date = new Date();
        return String.valueOf(date.getTime());
    }

    public static Long getNowTimestampToLong() {
        Date date = new Date();
        return date.getTime();
    }

    public static Long getTimestampToLong(LocalDateTime now) {
        return now.toEpochSecond(ZoneOffset.UTC);
    }

    public static LocalDateTime getTimestamp1500(String date8) {
        int year = Integer.parseInt(date8.substring(0, 4));
        int month = Integer.parseInt(date8.substring(4, 6));
        int dayOfMonth = Integer.parseInt(date8.substring(6, 8));
        return LocalDateTime.of(year, month, dayOfMonth, 15, 0, 0);
    }

    public static LocalDateTime getTimestamp(String date8, int hour, int minute, int second) {
        int year = Integer.parseInt(date8.substring(0, 4));
        int month = Integer.parseInt(date8.substring(4, 6));
        int dayOfMonth = Integer.parseInt(date8.substring(6, 8));
        return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
    }
    public static LocalDateTime getDate1500(LocalDateTime time) {
        return LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), 15, 0, 0);
    }

    public static LocalDateTime getDate(LocalDateTime time, int hour, int minute, int second) {
        return LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), hour, minute, second);
    }
    public static LocalDateTime getDate(LocalDateTime time, String hour, String minute, String second) {
        return LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second));
    }


    public static LocalDateTime getTimestamp8(String date8) {
        int year = Integer.parseInt(date8.substring(0, 4));
        int month = Integer.parseInt(date8.substring(4, 6));
        int dayOfMonth = Integer.parseInt(date8.substring(6, 8));
        return LocalDateTime.of(year, month, dayOfMonth, 0, 0, 0);
    }

    public static LocalDateTime getTimestamp(String date16) {
        if (Strings.isEmpty(date16) || date16.length() < 14) {
            return LocalDateTime.now();
        }
        int year = Integer.parseInt(date16.substring(0, 4));
        int month = Integer.parseInt(date16.substring(4, 6));
        int dayOfMonth = Integer.parseInt(date16.substring(6, 8));
        int hour = Integer.parseInt(date16.substring(8, 10));
        int minute = Integer.parseInt(date16.substring(10, 12));
        int second = Integer.parseInt(date16.substring(12, 14));
        return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
    }

    public static LocalDateTime timestampAdd(LocalDateTime timestamp, String frequency) {
        if (timestamp == null) {
            return null;
        }
        frequency = frequency.toLowerCase();
        long delta = -Long.parseLong(frequency.substring(0, frequency.length() - 1));

        if (frequency.endsWith("s")) {
            return timestamp.minusSeconds(delta);
        }

        if (frequency.endsWith("m")) {
            return timestamp.minusMinutes(delta);
        }

        if (frequency.endsWith("d")) {
            return timestamp.minusDays(delta);
        }
        return null;
    }


    public static boolean compare(LocalDateTime t1Start, LocalDateTime t2End, long deta, ChronoUnit detaUnit) {
        if (ChronoUnit.DAYS.compareTo(detaUnit) == 0) {
            t1Start = t1Start.withHour(0).withMinute(0).withSecond(0);
            t2End = t2End.withHour(0).withMinute(0).withSecond(0);
            return ChronoUnit.DAYS.between(t1Start, t2End) >= deta;
        }
        if (ChronoUnit.SECONDS.compareTo(detaUnit) == 0) {
            return ChronoUnit.SECONDS.between(t1Start, t2End) >= deta;
        }
        return false;
    }

    public static boolean before(String time16o1, String time16o2) {
        return CommonTimeUtil.getTimestamp(time16o1).isBefore(CommonTimeUtil.getTimestamp(time16o2));
    }

    public static void sleep(long m) {
        try {
            Thread.sleep(m);
        } catch (Exception e) {
        }
    }

}
